package com.fujitsu.sodoku.dialog;

import java.util.ArrayList;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;

import com.fujitsu.sodoku.R;
import com.fujitsu.sodoku.view.PuzzleView;

public class Keypad extends Dialog implements android.view.View.OnClickListener {
	private final ArrayList<View> keys = new ArrayList<View>();;
	private ArrayList<Integer> useds;
	private PuzzleView puzzleView;

	public Keypad(Context context) {
		super(context);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitle(R.string.keypad);
		setContentView(R.layout.keypad);
		initViews();
	}

	private void initViews() {
		keys.add(findViewById(R.id.keypad_1));
		keys.add(findViewById(R.id.keypad_2));
		keys.add(findViewById(R.id.keypad_3));
		keys.add(findViewById(R.id.keypad_4));
		keys.add(findViewById(R.id.keypad_5));
		keys.add(findViewById(R.id.keypad_6));
		keys.add(findViewById(R.id.keypad_7));
		keys.add(findViewById(R.id.keypad_8));
		keys.add(findViewById(R.id.keypad_9));
		for (int i = 0; i < keys.size(); i++) {
			View view = keys.get(i);
			view.setTag(i + 1);
			if (useds.contains(i + 1)) {
				view.setVisibility(View.INVISIBLE);
			}
			view.setOnClickListener(this);

		}
	}

	public void setButtonState(ArrayList<Integer> useds) {
		this.useds = useds;
		for (int i = 0; i < keys.size(); i++) {
			View view = keys.get(i);
			view.setTag(i + 1);
			view.setVisibility(View.VISIBLE);
			if (useds.contains(i + 1)) {
				view.setVisibility(View.INVISIBLE);
			}

		}

	}

	public Keypad(Context context, ArrayList<Integer> useds,
			PuzzleView puzzleView) {
		this(context);
		this.puzzleView = puzzleView;
		this.useds = useds;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.keypad_1:
			puzzleView.setSelectTitle(1);
			this.dismiss();
			break;
		case R.id.keypad_2:
			puzzleView.setSelectTitle(2);
			this.dismiss();
			break;
		case R.id.keypad_3:
			puzzleView.setSelectTitle(3);
			this.dismiss();
			break;
		case R.id.keypad_4:
			puzzleView.setSelectTitle(4);
			this.dismiss();
			break;
		case R.id.keypad_5:
			puzzleView.setSelectTitle(5);
			this.dismiss();
			break;
		case R.id.keypad_6:
			puzzleView.setSelectTitle(6);
			this.dismiss();
			break;
		case R.id.keypad_7:
			puzzleView.setSelectTitle(7);
			this.dismiss();
			break;
		case R.id.keypad_8:
			puzzleView.setSelectTitle(8);
			this.dismiss();
			break;
		case R.id.keypad_9:
			puzzleView.setSelectTitle(9);
			this.dismiss();
			break;
		default:
			this.dismiss();
			break;
		}

	}

}
